package GUI;

import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.ArrayList;

import logic.Book;
import logic.Message;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.UIManager;

public class Manage_Stock extends JPanel{
	private Client_GUI parent;
	private JTextField textField;
	private JLabel lblL;
	private int messageAction;
	
	private Manage_Stock self;
	
	private Book managedBook;
	
	public Manage_Stock(final Client_GUI parent,final int action) {
		this.parent = parent;
		this.self = this;
		this.setBackground(UIManager.getColor("inactiveCaption"));

		this.setSize(Client_GUI.MAIN_WIDTH, Client_GUI.MAIN_HEIGHT);
		setLayout(null);
		
		JLabel lblNewLabel = new JLabel("");
		lblNewLabel.setForeground(Color.RED);
		lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 18));
		lblNewLabel.setBounds(265, 51, 230, 56);
		add(lblNewLabel);
		
		JLabel lblNewLabel_1 = new JLabel("Book ID: ");
		lblNewLabel_1.setFont(new Font("Tahoma", Font.BOLD, 13));
		lblNewLabel_1.setBounds(115, 205, 58, 23);
		add(lblNewLabel_1);
		
		textField = new JTextField();
		textField.addKeyListener(new KeyAdapter() {
			@Override
			public void keyReleased(KeyEvent e) {
				ValidateData(false);
			}
		});
	
		textField.setBounds(184, 197, 198, 41);
		add(textField);
		textField.setColumns(10);
		
		JButton btnNewButton = new JButton("");
		btnNewButton.setFont(new Font("Tahoma", Font.BOLD, 14));
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				if (!textField.getText().equals("")){
					//TODO set managedbook
				}
				
				switch(action){
				case SearchBook.SEARCH_FOR_DELETE:{
					ValidateData(true);

					break;
				}
				
				case SearchBook.SEARCH_FOR_UPDATE:{
					Add_Book updater = new Add_Book(parent,managedBook);
					parent.RepaintMain(updater);
					
					break;
				}
				}
				
			}
		});
		btnNewButton.setBounds(178, 380, 168, 56);
		add(btnNewButton);
		
		JButton btnCancel = new JButton("Cancel");
		btnCancel.setFont(new Font("Tahoma", Font.BOLD, 14));
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				Home home = new Home(parent,parent.client.publicMessages);
				VerifyDialog verify = new VerifyDialog(parent, home,false);
			}
		});
		btnCancel.setBounds(481, 380, 168, 56);
		add(btnCancel);
		
		lblL = new JLabel("");
		lblL.setBounds(391, 197, 40, 41);
		add(lblL);
		
		JButton btnNewButton_1 = new JButton("Search book");
		btnNewButton_1.setFont(new Font("Tahoma", Font.BOLD, 12));
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				SearchBook topaint = new SearchBook(parent,action,self);
				parent.currentMain = topaint;
				parent.RepaintMain(topaint);
			}
		});
		btnNewButton_1.setBounds(495, 197, 154, 35);
		add(btnNewButton_1);
		
		switch(action){
		case SearchBook.SEARCH_FOR_DELETE:{
			lblNewLabel.setText("Delete Book");
			btnNewButton.setText("Delete Book");
			messageAction = Message.DELETE_BOOK_REQUEST;
			break;
		}
		
		case SearchBook.SEARCH_FOR_UPDATE:{
			lblNewLabel.setText("Update Book");
			btnNewButton.setText("Update Book");
			messageAction = Message.UPDATE_BOOK_REQUEST;
		}
		}
	}
	
	/**
	 * This function sets the returned BID
	 * @param book - the returned bid to put on the text field
	 */
	public void SetBID(Book book){
		this.managedBook = book;
		this.textField.setText(String.valueOf(book.getBID()));
	}
	
	
	private void ValidateData(boolean fromOK){
		
		ImageIcon okImageIcon = new ImageIcon(getClass().getClassLoader().getResource("resource/ok.png"));
		ImageIcon OK = new ImageIcon (okImageIcon.getImage().getScaledInstance(24, 24, java.awt.Image.SCALE_SMOOTH));
		
		ImageIcon badImageIcon = new ImageIcon(getClass().getClassLoader().getResource("resource/bad.png"));
		ImageIcon BAD = new ImageIcon (badImageIcon.getImage().getScaledInstance(24, 24, java.awt.Image.SCALE_SMOOTH));
		
		boolean valid = true;
		
		String temp; // the string to be tested on each test
		
		//Validate BOOK ID
		temp = textField.getText();
		if (!temp.equals(""))
		{
		try{
			char c = temp.charAt(0);
			if (c == '+' || c == '-') 
				{
				lblL.setIcon(BAD);
				valid = false;
				}
			else lblL.setIcon(OK);
			Integer.parseInt(temp);	
			}
		catch(NumberFormatException e)
			{
			lblL.setIcon(BAD);
			valid = false;
			}
		}
		else lblL.setIcon(null);
		
		
		
		
		
		
		if (fromOK){
		
			if (  temp.equals("") ){
				lblL.setIcon(BAD);
				valid = false;
			}
		}
		
		
		if (valid && fromOK){
			ArrayList<String> index = new ArrayList<String>();
			ArrayList<Object> data = new ArrayList<Object>();
			
			
			index.add("BID");
			data.add(textField.getText());
			
			int messageAction = Message.DELETE_BOOK_REQUEST;
			
			Message toSend = new Message(index, data, messageAction);
			
			if (parent.sendMessageToServer(toSend) == 1){//message sent successfully
				parent.SetLoading(true);
				
				//TODO handle accept request on server side, and then sent result
				
			}else{//message send failed
				//TODO verify extream situation
			}
			
		
	}


	}
	
}
